import { Controller, Get, Post, Body, Query, Put, Param, Delete } from '@nestjs/common';
import { ApiTags, ApiOperation, ApiBody, ApiConsumes, ApiQuery, ApiBearerAuth, ApiParam } from '@nestjs/swagger';
import { MenuService } from './menu.service';
import { CreateMenuDto, UpdateMenuDto, ListDeptDto } from './dto/index';
import { RequirePermission } from 'src/common/decorators/require-premission.decorator';
import { RepeatSubmit } from 'src/common/decorators/repeat-submit.decorator';
import { Operlog } from 'src/common/decorators/operlog.decorator';
import { BusinessType } from 'src/common/constant/business.constant';
import { SysMenuEntity } from './entities/menu.entity';
import { ApiDataResponse } from 'src/common/decorators/apiDataResponse.decorator';
import { UserTool, UserToolType } from '../user/user.decorator';

@ApiTags('菜单管理')
@Controller('system/menu')
export class MenuController {
  constructor(private readonly menuService: MenuService) {}

  @ApiOperation({ summary: '菜单管理-创建' })
  @ApiBody({ type: CreateMenuDto, required: true })
  @RequirePermission('system:menu:add')
  @Post()
  @RepeatSubmit()
  @Operlog({ businessType: BusinessType.INSERT })
  create(@Body() createMenuDto: CreateMenuDto, @UserTool() { injectCreate }: UserToolType) {
    return this.menuService.create(injectCreate(createMenuDto));
  }

  @ApiOperation({ summary: '菜单管理-列表' })
  @RequirePermission('system:menu:list')
  @Get('/list')
  @ApiQuery({ type: ListDeptDto })
  findAll(@Query() query: ListDeptDto) {
    return this.menuService.findAll(query);
  }

  @ApiOperation({ summary: '菜单管理-树表' })
  @RequirePermission('system:menu:query')
  @Get('/treeselect')
  treeSelect() {
    return this.menuService.treeSelect();
  }

  @ApiOperation({ summary: '菜单管理-角色-树表' })
  @RequirePermission('system:menu:query')
  @Get('/roleMenuTreeselect/:menuId')
  @ApiParam({ name: 'menuId', description: '菜单ID', required: true })
  roleMenuTreeSelect(@Param('menuId') menuId: string) {
    return this.menuService.roleMenuTreeselect(+menuId);
  }

  @ApiOperation({ summary: '菜单管理-详情' })
  @RequirePermission('system:menu:query')
  @ApiDataResponse(SysMenuEntity, false, false)
  @Get(':menuId')
  @ApiParam({ name: 'menuId', description: '菜单ID', required: true })
  findOne(@Param('menuId') menuId: string) {
    return this.menuService.findOne(+menuId);
  }

  @ApiOperation({ summary: '菜单管理-修改' })
  @ApiBody({ type: UpdateMenuDto, required: true })
  @RequirePermission('system:menu:edit')
  @Put()
  @RepeatSubmit()
  @Operlog({ businessType: BusinessType.UPDATE })
  update(@Body() updateMenuDto: UpdateMenuDto, @UserTool() { injectUpdate }: UserToolType) {
    return this.menuService.update(injectUpdate(updateMenuDto));
  }

  @ApiOperation({ summary: '菜单管理-删除' })
  @RequirePermission('system:menu:remove')
  @Delete(':menuId')
  @RepeatSubmit()
  @Operlog({ businessType: BusinessType.DELETE })
  @ApiParam({ name: 'menuId', description: '菜单ID', required: true })
  remove(@Param('menuId') menuId: string) {
    return this.menuService.remove(+menuId);
  }
}
